package com.thingsgrid.device.feign;

import com.thingsgrid.device.entity.Product;
import com.thingsgrid.device.entity.Topic;
import com.thingsgrid.device.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;


/*@RestController
@AllArgsConstructor
@Service*/
@ApiIgnore
@RestController
//@AllArgsConstructor
@ComponentScan("com.springblade.product.service")
public class ProductClient implements IProductClient {
	@Autowired
	private IProductService productService;

	//@Override
	@Override
	@GetMapping(API_PREFIX+"/detail")
	public Product productInfoById(@PathVariable(value = "productId") String productId) {
		return  productService.findProductById(productId);
	}


	/**
	 * 获取产品列表
	 *
	 * @param topicId
	 * @return 09.16
	 */
	@Override
	@GetMapping(API_PREFIX + "/findProductByTopicId")
	public List<Product> findProductByTopicId(String topicId) {
		List<Product> products1 = new ArrayList<>();
		List<Product> products = productService.findAll();
		for (Product product : products) {
			List<Topic> topics = product.getTopicItems();
			if (topics != null) {
				for (Topic topic : topics) {
					if (topic.getTopicId().equals(topicId)) {
						if (product != null){
							products1.add(product);
						}
						break;
					}
				}
			}
		}
		return products1;
	}
}
